﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(false)]
    public partial class code_signing : BaseForm
    {
        public code_signing()
            : base()
        {
        }

        public code_signing(DbContext dbContext)
            : base(dbContext)
        {

            cbProcedure.DataSource = Utility.proceudres(dbContext);
            cbProcedure.DisplayMember = "Name";

            cbPermissions.DataSource = Utility.permissions(dbContext);
            cbPermissions.DisplayMember = "Name";

            lbPermissions.Items.Clear();
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("USE MASTER;");
            sb.AppendLine("BEGIN TRY");
            sb.AppendLine("   BEGIN TRANSACTION");
            sb.AppendLine();
            sb.AppendLine("   CREATE CERTIFICATE [" + tbName.Text + "]");
            sb.AppendLine("      ENCRYPTION BY PASSWORD = '" + tbEncryptPassword.Text + "'");
            sb.AppendLine("      WITH SUBJECT = '" + tbSubject.Text + "';");
            sb.AppendLine();
            sb.AppendLine("   ADD SIGNATURE TO OBJECT::" + cbProcedure.SelectedText + "");
            sb.AppendLine("      BY CERTIFICATE [" + tbName.Text + "]");
            sb.AppendLine("      WITH PASSWORD = '" + tbEncryptPassword.Text + "';");
            sb.AppendLine();
            sb.AppendLine("   ALTER CERTIFICATE [" + tbName.Text + "]");
            sb.AppendLine("      REMOVE PRIVATE KEY;");
            sb.AppendLine();
            sb.AppendLine("   CREATE LOGIN [" + tbLogin.Text + "]");
            sb.AppendLine("      FROM CERTIFICATE [" + tbName.Text + "];");
            sb.AppendLine();
            if (lbPermissions.Items.Count > 0)
            {
                foreach (string item in lbPermissions.Items)
                {
                    if (item != "System.Data.DataRowView")
                        sb.AppendLine("   GRANT " + item + " TO [" + tbLogin.Text + "];");
                }
            }
            sb.AppendLine();
            sb.AppendLine("   COMMIT TRANSACTION");
            sb.AppendLine("END TRY");
            sb.AppendLine("BEGIN CATCH");
            sb.AppendLine("   ROLLBACK TRANSACTION");
            sb.AppendLine("   DECLARE @e nvarchar(max)");
            sb.AppendLine("   SET @e = error_message()");
            sb.AppendLine("   RAISERROR('%s', 16, 1, @e)");
            sb.AppendLine("END CATCH");

            tbCommand.Text = sb.ToString();
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbEncryptPassword_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbSubject_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbProcedure_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbLogin_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbPermissions_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!lbPermissions.Items.Contains(cbProcedure.Text))
            {
                lbPermissions.Items.Add(cbPermissions.Text);
            }

            Write();
        }
    }
}